 /*
 * 新建产品管理视图
 * @Author: LuoQianJun
 * @Date: 2018-01-25 15:36:18
 * @Last Modified by: ljb
 * @Last Modified time: 2018-02-25 18:07:21
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-product-new-views {
        padding: 20px;
    }

</style>

 <template>

    <div class="v-admin-product-new-views">

        <!-- 面包屑组件 -->
        <vBreadcrumb :breadcrumbList="breadcrumbList"></vBreadcrumb>

        <Tabs type="card" @on-click="changeTab">

            <TabPane label="新增" name="新增">
                
                <product-create ref="create" 
                                :categories="categories"
                                @post="postData">
                </product-create>

            </TabPane>

            <TabPane label="查看" name="查看">

                <!-- 搜索组件 -->
                <search-form  @search="confirmSearch" :categories="categories"></search-form>
                
                <product-index  :loading="table_loading"
                                :categories="category_types"
                                :product="products.data"
                                @edit="productDetail"
                                @delete="deleteProduct">                       
                </product-index>
                <br>

                <!-- 分页组件 -->
                <Row type="flex" justify="center">
                    <Page @on-change="pageChange" 
                        @on-page-size-change="perPageChange"
                        placement="top"
                        :total="products.total" 
                        :current="products.current_page"
                        :page-size="query_info.per_page"
                        show-sizer show-total></Page>
                </Row>

            </TabPane>
        </Tabs>

        <Modal  title="修改产品"
                v-model="show_modal"
                width="768">

            <!-- 产品编辑组件 -->
            <product-edit   ref="edit"
                            :categories="categories"
                            :product-detail="product_detail"
                            @update="updateProduct"
                            @delete="deleteInformation"></product-edit>

            <div slot="footer"></div>

        </Modal>

    </div>

 </template>
 
 <script>

import API_LIST from 'src/API_LIST';

import vBreadcrumb from 'src/views/component/breadcrumb';

import { mapGetters , mapMutations } from "vuex";

import qs from 'qs';

import productCreate from './create';
import productIndex from './index';
import productEdit from './edit';
import searchForm from './search-form';

 export default {

    name: 'product',

    components : {

        vBreadcrumb,

        productCreate,

        productIndex,

        productEdit,

        searchForm
    
    },
    data: function() {	

         return {

            breadcrumbList: [
                {
                    name : '管理员',
                    href : '#',
                },
                {
                    name : '产品管理',
                    href : '#',
                },
                {
                    name: '新建产品',
                    href: '#',
                }
            ],

            query_info : {

                page : 1,

                per_page : 20,

                model_number : '',

                product_category_id: '',

                title : '',

            },
            
            search_info : {

                page : 1
                
            },                      //分页信息

            table_loading : false,      //表格更新时的loading

            show_modal : false,         //弹框的控制

            product_detail : {},        //产品详情

            products : {},     //产品列表

            category_types : {},    //列表展示所需产品种类

            categories: [],         //创建、编辑、搜索产品种类
         }
    },
    mounted () {
        this.fetchProductCategory();
    },
    computed: {
        ...mapGetters('userToken',[
            'getToken'
        ])  
    },
    methods: {

        ...mapMutations('spinShow',[
            'showSpin',
            'closeSpin'
        ]),

         /**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined} 
         */
        changeTab(tab_name) {
            
            if( tab_name === '查看' ) {

                this.fetchProductionList(this.query_info);

            }else {

                this.products.data = [];

            }

        },

        /**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined} 
         */
        pageChange(pageNumber) {

            this.search_info.page = pageNumber;

            this.search_info.per_page = this.query_info.per_page;

            this.fetchProductionList(this.search_info);
        },

        /**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
        perPageChange(per_page) {

            this.search_info.per_page = per_page;

            this.query_info.per_page = per_page;

            this.fetchProductionList(this.search_info);

        },
        
        /**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
        confirmSearch(param) {

            this.query_info.model_number = param.model_number;

            this.query_info.product_category_id = param.product_category_id;

            this.query_info.title = param.title;

            this.search_info = JSON.parse(JSON.stringify(this.query_info));

            this.fetchProductionList(this.query_info);

        },

        /**
         * 新增产品的post数据
         * @return {undefined}
         */
        postData(param){

            this.showSpin('请稍后');

            this.$http.post( API_LIST.PRODUCTION , param )
                    .then(({ data })=>{
                                
                        if( parseInt(data.code) !== 0 )throw new Error(data.error);

                        this.$Message.success('新增成功！');

                        this.$refs.create.handleReset('formValidate');
                                
                    })
                    .catch((err)=>{

                        this.$Message.error(err.message);
                
                    })
                    .then(this.closeSpin)

        },

        /**
         * 查看产品详情
         * @param  {string} id {合作伙伴的id}
         * @return {undefined}
         */
        productDetail(id){

            this.product_detail = this.products.data.filter((product) => product.id === id)[0];

            this.show_modal = true;

        },

        /**
         * 更新产品信息
         * @param  {Object} param {需要更细的参数对象}
         * @return {undefined}
         */
        updateProduct(id,param){

            this.showSpin();

            this.$http.post( API_LIST.PRODUCTION + '/' + id, param )
                .then(({ data })=>{
                            
                    if  ( parseInt(data.code) !== 0 )throw new Error(data.error);

                    this.$Message.success('更新成功！');

                    this.show_modal = false;

                    this.fetchProductionList(this.search_info);
                            
                })
                .catch((err)=>{

                    this.$Message.error(err.message || err.statusText);
            
                })
                .then(this.closeSpin)

        },

        /**
         * 删除产品的资料
         * @return {undefined}
         */
        deleteInformation(){

            if( !this.product_detail.information ){

                this.product_detail.information = '';

                this.$refs.edit.clearInformation();

                return;

            }

            this.showSpin('请稍后');

            const param = {
                id : this.product_detail.id
            }

            const query_str = qs.stringify(param);

            this.$http.get( API_LIST.DELETE_INFORMATION + '?' + query_str )
                .then(({ data })=>{
                            
                    if( parseInt(data.code) !== 0 )throw new Error(data.error);

                    this.$Message.success('删除资料成功！');

                    this.product_detail.information = '';

                    this.$refs.edit.clearInformation();
                            
                })
                .catch((err)=>{
            
                    this.$Message.error(err.message || err.statusText);

                })
                .then(this.closeSpin)

        },

        /**
         * 删除产品
         * @param  {string} id {需要删除的产品的id}
         * @return {undefined}
         */
        deleteProduct(id){

            this.$Modal.confirm({
                title: '确定删除此产品么？',
                onOk: () => {

                    const param = {
                        _method : 'DELETE'
                    }

                    this.table_loading = true;

                    this.$http.post( API_LIST.PRODUCTION + '/' + id , param )
                        .then(({ data })=>{
                                    
                            if( parseInt(data.code) !== 0 )throw new Error(data.error);

                            this.fetchProductionList(this.search_info);

                            this.$Message.success('删除成功');
                                    
                        })
                        .catch((err)=>{
                    
                            this.$Message.error(err.message || err.statusText);

                        })
                        .then(() => this.table_loading = false)
                    
                },
                onCancel: () => {}
            });

        },

        /**
         * 获取产品种类
         * @return {undefined}
        */
        fetchProductCategory() {

            this.showSpin('请稍后');

            this.$http.get(API_LIST.PRODUCTION + '/create')
                .then(({ data })=>{
                    if( parseInt(data.code) !== 0 )throw new Error(data.error);
                    this.categories = data.categories;
                    this.category_types = data.category_types;
                })
                .catch((err)=>{
                    this.$Message.error(err.message || err.statusText);
                })
                .then(this.closeSpin)
        },


        /**
         * 获取产品列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
        fetchProductionList(param){

            this.table_loading = true;

            let query_str = qs.stringify(param);

            this.$http.get( API_LIST.PRODUCTION + '?' + query_str )
                .then(({ data })=>{
                            
                    if( parseInt(data.code) !== 0 )throw new Error(data.error);
                    
                    this.products = data.products;
                                         
                })
                .catch((err)=>{
            
                    this.$Message.error(err.message);

                })
                .then(() => this.table_loading = false)

        }
     }
 }
 </script>
